package com.flyee.utils{
	import flash.utils.describeType;
	import flash.utils.getQualifiedClassName;
	
	
	public class CodeUtil{
		
		/**
		 * 构造函数,把对象转换成字符串的方法
		 * @param msg 数据对象
		 * @return 转换生成的字符串
		 * 
		 */	
		public function CodeUtil(){
		}

		public static var printStr:String = "";
					
		public static function makeStr(msg:Object):String
		{
			var str:String = "";
			var xml:XML = describeType(msg);
			var list:XMLList = xml.variable;
			str += "{";
			for(var i:int = 0; i < list.length();i++)
			{
				str += list[i].@name + ":";
				if(list[i].@type == "int"||list[i].@type == "uint"||list[i].@type == "Number"||list[i].@type == "Boolean")
				{
					str += dealSample(msg[list[i].@name]);
				}
				else if(list[i].@type == "String")
				{
					str += dealString(msg[list[i].@name]);
				}
				else if(list[i].@type == "XML")
				{
					str += dealXML(msg[list[i].@name]);
				}
				else if(list[i].@type == "Date")
				{
					str += dealDate(msg[list[i].@name]);
				}
				else if(list[i].@type == "Array")
				{
					str += dealArray(msg[list[i].@name]);
				}
				else
				{
					str += dealObject(msg[list[i].@name]);
				}
				if(i < (list.length() - 1))
				{
					str += ";"
				}				
			}
			str +=  "}";
			if(str.indexOf("type:110") == -1)
			printStr += ("@" + str + "\n");		
			return str;
		}
		/**
		 * 把字符串中的单引号和反斜杠转换成转译字符的方法 
		 * @param msg 传入的字符串
		 * @return 处理完后的字符串
		 * 
		 */		
		private static function translate(msg:String):String
		{
			var str:String = msg;
			var temp:Array = new Array();
			//处理反斜杠
			if(str.indexOf("\\") != -1)
			{
				temp = str.split("\\");
				str = "";
				for(var i:int = 0;i<temp.length;i++)
				{
					str += temp[i];
					if(i < (temp.length - 1))
					{
						str += "\\\\";
					}
				}
			}
			//处理单引号
			if(str.indexOf("'") != -1)
			{
				temp = str.split("'");
				str = "";
				for(var j:int = 0;j<temp.length;j++)
				{
					str += temp[j];
					if(j < (temp.length - 1))
					{
						str += "\\\'";
					}
				}
			}
			return str;
		}
		/**
		 * 处理简单类型的方法
		 * @param message 要处理的信息
		 * @return 转换生成的字符串
		 * 
		 */		
		private static function dealSample(message:*):String
		{
			if(message == null)
			{
				return null;
			}
			return  message.toString();			
		}
		/**
		 * 处理日期的方法 
		 * @param message 要处理的信息
		 * @return 转换生成的字符串
		 * 
		 */		
		private static function dealDate(message:Date):String
		{
			if(message == null)
			{
				return null;
			}
			return  message.getTime().toString();
		}
		/**
		 * 处理字符串的方法 
		 * @param message 要处理的信息
		 * @return 转换生成的字符串
		 * 
		 */		
		private static function dealString(message:String):String
		{
			if(message == null)
			{
				return null;
			}
			return "'" + translate(message) + "'";
		}
		/**
		 * 处理XML的方法
		 * @param message 要处理的信息
		 * @return 转换生成的字符串
		 * 
		 */		
		private static function dealXML(message:XML):String
		{
			if(message == null)
			{
				return null;
			}
			return "'" + translate(message.toString()) + "'";
		}
		/**
		 * 处理数组的方法
		 * @param message 要处理的信息
		 * @return 转换生成的字符串
		 * 
		 */		
		private static function dealArray(message:Array):String
		{
			if(message == null || message.length == 0)
			{
				return null;
			}
			var str:String = "[";
			for(var i:int = 0;i<message.length;i++)
			{
				if(typeof message[i] == "uint"||typeof message[i] == "int"||typeof message[i] == "number"||typeof message[i] == "boolean")
				{
					str += dealSample(message[i]) + ",";
				}
				else if(typeof message[i] == "string")
				{
					str += dealString(message[i]) + ",";
				}
				else if(typeof message[i] == "xml")
				{
					str += dealXML(message[i]) + ",";
				}
				else if(getQualifiedClassName(message[i]) == "Array")
				{
					str += dealArray(message[i]) + ",";
				}
				else
				{
					str += dealObject(message[i]) + ",";
				}
			}
			str = str.substring(0,str.length -1) + "]";
			return str;
		}
		/**
		 * 处理对象的方法
		 * @param message 要处理的信息
		 * @return 转换生成的字符串
		 * 
		 */		
		private static function dealObject(message:Object):String
		{
			return makeStr(message);
		}
		
	}
	
}